package com.neuedu.user.controller;

import com.neuedu.po.User;
import com.neuedu.user.service.UserService;
import com.neuedu.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    @Autowired
    private UserServiceImpl userServiceImpl;

    @RequestMapping("/selectAll")
    // http://localhost:8088/user/selectAll?uname=t&homecall=fa
    public List<User> selectAll(User u){
        return service.selectAll(u);
    }

    @RequestMapping("/add")
    // http://localhost:8088/user/add?uname=marry&pass=888&&account=12345678904&sal=5000.0&homecall=sister&age=20&power=user
    public int add(User u){
        return service.add(u);
    }

    @RequestMapping("/update")
    // http://localhost:8088/user/update?uname=sophia&pass=888&&account=12345678904&sal=5000.0&homecall=sister&age=20&power=user&id=4
    public int update(User u){
        return service.update(u);
    }

    @RequestMapping("/delete")
    // http://localhost:8088/user/delete?id=4
    public int delete(User u){
        return service.delete(u);
    }

    @RequestMapping("/login")
    // http://localhost:8088/user/login?uname=tom&pass=123
    public User login(User u, HttpSession session){
        User user = service.login(u);
        if(user!=null){
            session.setAttribute("user",user);
        }
        return user;
    }

    @RequestMapping("/logout")
    // http://localhost:8088/user/logout
    public String logout(HttpSession session){
        session.invalidate();
        return "logout_OK !";
    }

}
